<?php

namespace app\api\model;

use app\admin\controller\Member;
use app\admin\model\MemberModel;
use app\api\controller\PayCall;
use think\Db;
use think\Exception;
use think\Model;


class GoodsOrderModel extends ApiModel
{


    // 表名
    protected $name = 'goods_order';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'create_time';
    protected $updateTime = false;
    protected $deleteTime = false;


    protected $status = [0 => '未支付', 1 => '待发货', 2 => "已发货", 3 => "交易完成", 4 => "取消订单"];




    public function member()
    {
        return $this->belongsTo(MemberModel::class, 'member_id', 'id', [], 'LEFT');
    }

    public function getGoodsOrder($params)
    {
        $status = $params['status'] ?? '';
        if($params['status'] != ''){
            $map['status'] = $status;
        }
        $map['member_id'] =$params['uid'];
        $order = self::where($map)
            ->field('id,out_trade_no,nums,goods_name,goods_image,
            goods_price,pay_amount,goods_cate_name,phone,realname,address,notes,status,create_time,pay_time
            ,express_name,express_nums,pay_type')
            ->order('create_time desc')
            ->paginate(10);
        foreach ($order as &$value) {
            $value['status_name'] = $this->status[$value['status']] ?? '';

            $value['pay_time'] = $value['pay_time'] ? date('Y-m-d H:i:s', $value['pay_time']) : '';

            $value['goods_image'] = config('app.image_url') . $value['goods_image'];
        }

        return $this->api_success('获取成功', $order);
    }


    public function getGoodsOrderDetail($params = array()){
        $order_id = $params['order_id'] ?? '';
        $order = self::where(['out_trade_no' => $order_id])
            ->field('id,out_trade_no,nums,goods_name,goods_image,
            goods_price,pay_amount,goods_cate_name,phone,realname,address,notes,status,create_time,pay_time
            ,express_name,express_nums,pay_type')
            ->find();
        if (!$order) {
            return $this->api_error('订单不存在');
        }
        $order['status_name'] = $this->status[$order['status']] ?? '';
        $order['goods_image'] = config('app.image_url') . $order['goods_image'];
        $order['pay_time'] = $order['pay_time'] ? date('Y-m-d H:i:s', $order['pay_time']) : '';

        return $this->api_success('获取成功', $order);
    }


    public function cancelGoodsOrder($params)
    {
        $order_id = $params['order_id'] ?? '';
        $order = self::where(['out_trade_no' => $order_id, 'status' => [0, 1]])->find();
        if (!$order) {
            return $this->api_error('该订单不能取消');
        }
        try {
            Db::startTrans();
            self::where(['out_trade_no' => $order_id])->update(['status' => 4]);
            //添加库存
            (new GoodsModel())->where(['id' => $order['goods_id']])->setInc('residue_stock', $order['nums']);

            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return $this->api_error('操作失败');
        }

        return $this->api_success('取消成功');

    }


    //确认收获
    public function confirmGoodsOrder($params)
    {
        $order_id = $params['order_id'] ?? '';
        $order = self::where(['out_trade_no' => $order_id, 'status' => 2])->find();
        if (!$order) {
            return $this->api_error('该订单不能操作');
        }
        try {
            Db::startTrans();
            //更新订单
            self::where(['out_trade_no' => $order_id])->update(['status' => 3]);
            //添加分销
            (new PayCall())->retail($order_id, $order['member_id'], $order['pay_amount'], 'goods');
            Db::commit();
            return $this->api_success('操作成功');
        } catch (Exception $e) {
            Db::rollback();
            return $this->api_error('操作失败');
        }

    }

}
